/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package auth;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

/**
 *
 * @author root
 */
@WebService()
public class AuthenticationWS {

    /**
     * Web service operation
     */
    @WebMethod(operationName = "authentication")
    public boolean authentication(@WebParam(name = "id")
    String id, @WebParam(name = "pwd")
    String pwd) {
        try {
            Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
            Connection connexion = DriverManager.getConnection("jdbc:derby://localhost:1527/TestDB", "admin", "admin");
            PreparedStatement stmt;
            ResultSet result;
            boolean found;
            stmt = connexion.prepareStatement("select * from APP.USERS u " + "where u.id='" + id + "' and u.password='" + pwd + "'", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            result = stmt.executeQuery();
            if (result.first()) {
                found = true;
            } else {
                found = false;
            }
            result.close();
            stmt.close();
            connexion.close();
            return found;
            
        } catch (SQLException ex) {
            Logger.getLogger(AuthenticationWS.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(AuthenticationWS.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
        /*if(id.equals("viet") && pwd.equals("viet12345"))
            return true;
        else  return false;*/
    }

}
